class AsyncSeriesHook {
    constructor() {
        this.tasks = [];
    }
    tapAsync(name, task) {
        this.tasks.push(task);
    }
    callAsync(...args) {
        const endCb = args.pop();
        let index = 0;
        // 递归
        let next = () => {
            if (index === this.tasks.length) {
                return endCb();
            }
            let task = this.tasks[index];
            index++;
            task(...args, next);
        }
        next();
    }

    tapPromise(name, task) {
        this.tasks.push(task);
    }
    promise(...args) {
        const [first, ...others] = this.tasks;
        return others.reduce((pre, next) => {
            return pre.then(() => next(...args));
        }, first(...args));
    }
}
module.exports = AsyncSeriesHook;